﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web;
using System.Text.RegularExpressions;
using System.IO;
using KC.DataAccess.MV6;
using KC.DataAccess.BuggerAll;
using KC.ExtensionMethods;

namespace KC.Business.Meta.Jobs
{
	public class HeuristicsJob : JobBase 
	{
		protected override void PreProcess_Global()
		{
		}
		protected override void Process_Global()
		{
			UpdateResults(DetectSpidersInIISLogs(),false);
		}
		protected override void PostProcess_Global()
		{
		}

		#region Implementation

		/// <summary>Detects spiders based on IIS logs.</summary>
		/// <returns>Report of spider detection</returns>
		private static string DetectSpidersInIISLogs()
		{
			List<string> files = new List<string>();
			foreach (string file in System.IO.Directory.GetFiles(KC.Constants.FilePaths.IisLogPath))
				if (DateTime.Now - new FileInfo(file).LastWriteTime < TimeSpan.FromDays(30))
					files.Add(file);

			int SpidersProcessed = 0;
			foreach (string file in files)
				foreach (string line in System.IO.File.ReadAllLines(file))
					if (!line.StartsWith("#") && line.ToLower().Contains("get /robots.txt"))
						if (Utils.ProcessSpider(line))
							SpidersProcessed++;

			return "Processed " + SpidersProcessed.ToString() + " spiders;";
		}

		#endregion Implementation
	}
}
